package RPPM.dao.impl;

import RPPM.dao.ExecutorDao;
import RPPM.domain.*;
import RPPM.util.JDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;
import java.util.Map;

public class ExecutorDaoImpl implements ExecutorDao {
    private JdbcTemplate jdbcTemplate =new JdbcTemplate(JDBCUtils.getDataSource());
    /**
     * 登录
     */
    @Override
    public Executor findExecutorById(Executor tobechecked) {

        String sql = "select * from executor where Exe_id = ?";
        Executor executor1 = null;
        System.out.println(tobechecked.getExe_id());
        try {

            executor1 = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<Executor>(Executor.class), tobechecked.getExe_id());

        } catch (Exception e){
            executor1 = null;
            return executor1;
        }
        return executor1;
    }

    /**
     * 展示课程资料
     */
    @Override
    public List<Map<String, Object>> ShowCourse() {
        System.out.println("ShowCourse");
        String sql = "SELECT Cou_id, Cou_name, Cou_content, Com_name, Com_phone " +
                "FROM course" +
                " WHERE Cou_pass = 1;";

        List<Map<String, Object>> courses = jdbcTemplate.queryForList(sql);

        return courses;
    }

    @Override
    public List<Map<String, Object>> findCoursesByCouname(String searchtext) {
        String sql = "SELECT Cou_id, Cou_name, Cou_content, Com_name, Com_phone " +
                "FROM course " +
                "WHERE Cou_pass = 1 " +
                "AND Cou_name LIKE ?";
        return jdbcTemplate.queryForList(sql,"%"+searchtext+"%");
    }

    @Override
    public List<Map<String, Object>> getNoPassPlan() {
        String sql = "SELECT Cou_id, Cou_name, Cou_content, Com_name, Com_phone " +
                "FROM course " +
                "WHERE Cou_pass = 0";

        return jdbcTemplate.queryForList(sql);
    }

    @Override
    public boolean passPlan(Map<String, Object> map) {
        String sql = "update course set Cou_pass = 1 where Cou_id = ? and Cou_name = ? and Com_name = ? and Com_phone = ?";
        return (jdbcTemplate.update(sql, map.get("Cou_id"),  map.get("Cou_name"), map.get("Com_name"),map.get("Com_phone"))==1);
    }

    @Override
    public List<Map<String, Object>> findCoursesNotPassedByCouname(String searchtext) {
        String sql = "SELECT Cou_id, Cou_name, Cou_content, Com_name, Com_phone " +
                "FROM course " +
                "WHERE Cou_pass = 0 " +
                "AND Cou_name LIKE ?";
        return jdbcTemplate.queryForList(sql,"%"+searchtext+"%");
    }

}
